<?php

namespace api\models;

use Yii;
use yii\web\IdentityInterface;
use yii\filters\RateLimitInterface;


/**
 * This is the model class for table "{{%user}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $header
 * @property string $access_token
 * @property string $unionid
 * @property integer $token_expire
 * @property integer $award_num
 * @property integer $bet_num
 * @property integer $bet_win
 * @property integer $status
 */
class User extends \common\models\User implements IdentityInterface,RateLimitInterface
{
    public static function findIdentity($id){
        return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
    }
    public static function findIdentityByAccessToken($token, $type = null){
        return static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
    }
    public function getId(){
        return $this->getPrimaryKey();
    }

    public function getAuthKey(){
        return 'AuthKey';
    }
    public function validateAuthKey($authKey){
        return true;
    }


    public function getRateLimit($request, $action){
        return [15, 10];
    }
    public function loadAllowance($request, $action){}
    public function saveAllowance($request, $action, $allowance, $timestamp){}

    public static function getUserByUnionId($unionId = ''){
        $user = User::findOne([
            'unionid' => trim($unionId)
        ]);
        return $user ? $user : [];
    }

    public static function getUserByAccessToken($accessToken){
        $user = User::findOne([
            'access_token' => trim($accessToken)
        ]);
        return $user ? $user : [];
    }
}
